package org.esisar.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.esisar.dao.JDBCDAO;
import org.esisar.dao.SearchAlbumStatementCreator;
import org.esisar.dao.SearchStatementCreator;
import org.esisar.model.Album;
import org.esisar.model.Picture;

public class SearchController extends HttpServlet {


	private final JDBCDAO jdbcdao = new JDBCDAO();

	public void doGet(HttpServletRequest req, HttpServletResponse res) 
			throws ServletException, IOException{

		String searchType = req.getParameter("searchType");
		String search = req.getParameter("search");	
		if (searchType.equals("photo")){
			searchPhoto(req, res, search);
		}
		else if (searchType.equals("album")){
			try {
				searchAlbum(req,res,search);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}


	}

	private void searchAlbum(HttpServletRequest req, HttpServletResponse res,
			String search) throws SQLException {
		List<Album> albums = new ArrayList<Album>();
		
		foundAlbums(search, albums);
		
		req.setAttribute("albums", albums);

		RequestDispatcher dispatcher = req.getRequestDispatcher("/Album/getAlbums.jsp");
		try {
			dispatcher.forward(req, res);
		} catch (ServletException | IOException e) {
			e.printStackTrace();
		}

	}

	private void foundAlbums(String search, List<Album> albums) throws SQLException {
		
		albums.addAll((List<Album>)(List<?>) jdbcdao.findAll(new SearchAlbumStatementCreator(), "title", search));
		albums.addAll((List<Album>)(List<?>) jdbcdao.findAll(new SearchAlbumStatementCreator(), "subtitle", search));
	
		ListIterator<Album> listIterator = albums.listIterator();
		
			while(listIterator.hasNext()){
				Album album = listIterator.next();
			if (!album.isShared()){
				listIterator.remove();
			}
		}		
	}

	private void searchPhoto(HttpServletRequest req, HttpServletResponse res,
			String search) throws ServletException, IOException {
		try {

			List<Picture> photos = new ArrayList<Picture>();

			foundPictures(search, photos);


			req.setAttribute("photos", photos);

			RequestDispatcher dispatcher = req.getRequestDispatcher("/Photo/getPhotos.jsp");
			dispatcher.forward(req, res);

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private void foundPictures(String search, List<Picture> photos) throws SQLException {
		searchByField("camera", search, photos);
		searchByField("focal", search, photos);
		searchByField("sensitivity", search, photos);
		searchByField("opening", search, photos);
		searchByField("resolution", search, photos);
		searchByField("obturation", search, photos);
	}

	private void searchByField(String field, String value, List<Picture> photos)
			throws SQLException {
		photos.addAll((List<Picture>)(List<?>) jdbcdao.findAll(new SearchStatementCreator(), field, value));
		
	}

}
